package cn.cttic.order.controller;

import java.util.List;

import cn.cttic.order.api.domain.BaseInstallOrderSubPage;
import cn.cttic.order.api.domain.bo.*;
import cn.cttic.order.api.domain.vo.BaseInstallOrderMainPageVo;
import cn.cttic.order.api.domain.vo.BaseInstallOrderSubPageVo;
import cn.cttic.order.api.domain.vo.BaseInstallOrderVo;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.cttic.common.idempotent.annotation.RepeatSubmit;
import cn.cttic.common.log.annotation.Log;
import cn.cttic.common.web.core.BaseController;
import cn.cttic.common.mybatis.core.page.PageQuery;
import cn.cttic.common.core.domain.R;
import cn.cttic.common.core.validate.AddGroup;
import cn.cttic.common.core.validate.EditGroup;
import cn.cttic.common.log.enums.BusinessType;
import cn.cttic.common.excel.utils.ExcelUtil;
import cn.cttic.order.api.domain.vo.BaseInstallOrderProcessVo;
import cn.cttic.order.service.IBaseInstallOrderProcessService;
import cn.cttic.common.mybatis.core.page.TableDataInfo;

/**
 * 安装数据
 * 前端访问路由地址为:/order/installOrderProcess
 *
 * @author Times.Studio
 * @date 2024-09-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/installOrderProcess")
public class BaseInstallOrderProcessController extends BaseController {

    private final IBaseInstallOrderProcessService baseInstallOrderProcessService;

    /**
     * 查询安装数据列表
     */
    @SaCheckPermission("order:installOrderProcess:list")
    @GetMapping("/list")
    public TableDataInfo<BaseInstallOrderProcessVo> list(BaseInstallOrderProcessBo bo, PageQuery pageQuery) {
        return baseInstallOrderProcessService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出安装数据列表
     */
    @SaCheckPermission("order:installOrderProcess:export")
    @Log(title = "安装数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseInstallOrderProcessBo bo, HttpServletResponse response) {
        List<BaseInstallOrderProcessVo> list = baseInstallOrderProcessService.queryList(bo);
        ExcelUtil.exportExcel(list, "安装数据", BaseInstallOrderProcessVo.class, response);
    }

    /**
     * 新增安装订单
     */
    @RepeatSubmit()
    @PostMapping("/mainPageOcr")
    public R<BaseInstallOrderMainPageVo> mainPageOcr(@Validated @RequestBody MainPageOcrBo bo) {
        try {
            BaseInstallOrderMainPageVo vo = baseInstallOrderProcessService.mainPageOcr(bo);
            if (ObjectUtil.isNotEmpty(vo)) {
                return R.ok(vo);
            } else {
                return R.fail("识别失败");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.fail(ex.getMessage());
        }
    }

    /**
     * 新增安装订单
     */
    @RepeatSubmit()
    @PostMapping("/subPageOcr")
    public R<BaseInstallOrderSubPageVo> subPageOcr(@Validated @RequestBody SubPageOcrBo bo) {
        try {
            BaseInstallOrderSubPageVo vo = baseInstallOrderProcessService.subPageOcr(bo);
            if (ObjectUtil.isNotEmpty(vo)) {
                return R.ok(vo);
            } else {
                return R.fail("识别失败");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.fail(ex.getMessage());
        }
    }

    /**
     * 新增安装订单
     */
    @SaCheckPermission("order:installOrder:add")
    @Log(title = "安装订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/audit")
    public R<BaseInstallOrderProcessVo> verify(@Validated @RequestBody AuditOrderProcessBo bo) {
        try {
            BaseInstallOrderProcessVo vo = baseInstallOrderProcessService.auditOrderProcess(bo);
            return R.ok(vo);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.fail(ex.getMessage());
        }
    }

    /**
     * 新增安装订单
     */
    @RepeatSubmit()
    @PostMapping("/updateFixOssId")
    public R<BaseInstallOrderProcessVo> updateFixOssId(@RequestBody BaseInstallOrderProcessBo bo) {
        try {
            BaseInstallOrderProcessVo vo = baseInstallOrderProcessService.updateFixOssId(bo);
            return R.ok(vo);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.fail(ex.getMessage());
        }
    }

    /**
     * 获取安装数据详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("order:installOrderProcess:query")
    @GetMapping("/{id}")
    public R<BaseInstallOrderProcessVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(baseInstallOrderProcessService.queryById(id));
    }

    /**
     * 新增安装数据
     */
    @SaCheckPermission("order:installOrderProcess:add")
    @Log(title = "安装数据", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseInstallOrderProcessBo bo) {
        return toAjax(baseInstallOrderProcessService.insertByBo(bo));
    }

    /**
     * 修改安装数据
     */
    @SaCheckPermission("order:installOrderProcess:edit")
    @Log(title = "安装数据", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseInstallOrderProcessBo bo) {
        return toAjax(baseInstallOrderProcessService.updateByBo(bo));
    }

    /**
     * 删除安装数据
     *
     * @param ids 主键串
     */
    @SaCheckPermission("order:installOrderProcess:remove")
    @Log(title = "安装数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(baseInstallOrderProcessService.deleteWithValidByIds(List.of(ids), true));
    }
}
